//
//  XS_NVC.swift
//  EKLWisdomP3
//
//  Created by 韩云智 on 2020/10/16.
//

import UIKit
import RxSwift
import RxCocoa

extension UINavigationController {
    // 去掉重复控制器
    func deduplication(_ vc: UIViewController) {
        let vcs = viewControllers.filter { !$0.isKind(of: vc.classForCoder) || $0 === vc }
        setViewControllers(vcs, animated: false)
    }
}

class XS_NVC: UINavigationController {
    static let shared = XS_NVC(rootViewController: XS_TBC())
    
    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        modalPresentationStyle = .fullScreen
        modalTransitionStyle = .flipHorizontal
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 13.0, *) { overrideUserInterfaceStyle = .light }
        // 导航栏统一样式
        navigationBar.isTranslucent = false
        navigationBar.backgroundColor = .white
        navigationBar.barStyle = .default
        navigationBar.shadowImage = UIImage()
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.tintColor = #colorLiteral(red: 0.2588235294, green: 0.2588235294, blue: 0.2588235294, alpha: 1)
        
        navigationBar.titleTextAttributes = [
            .font : UIFont.xsFont(name: .semibold, 18),
            .foregroundColor : #colorLiteral(red: 0.2588235294, green: 0.2588235294, blue: 0.2588235294, alpha: 1)
        ]
        
        interactivePopGestureRecognizer?.delegate = self
    }

    override var shouldAutorotate: Bool {
        get {
            guard let last = viewControllers.last else { return false }
            return last.shouldAutorotate
        }
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            guard let last = viewControllers.last else { return .allButUpsideDown }
            return last.supportedInterfaceOrientations
        }
    }
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        get {
            guard let last = viewControllers.last else { return UIApplication.shared.statusBarOrientation }
            return last.preferredInterfaceOrientationForPresentation
        }
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let last = viewControllers.last, (last.isKind(of: XS_LoginVC.self) || last.isKind(of: XS_Q1VC.self) || last.isKind(of: XS_ExclusivePlanVC.self)) {
            return false
        }
        return viewControllers.count > 1
    }
}

extension XS_NVC {
    /// 跳转登录
    /// - Parameter isOffsite: 是否重置项目
    func toLogin(_ isOffsite: Bool = false) {
        guard let top = topViewController, !top.isKind(of: XS_LoginVC.self) else { return }
        if let pvc = presentedViewController { pvc.dismiss(animated: false) }
        if isOffsite { setViewControllers([XS_TBC()], animated: false) }
        XS_Singleton.shared.infoModel = nil
        pushViewController(XS_LoginVC(), animated: false)
    }
}
